const app = getApp()
const db = wx.cloud.database()
let id = ''
Page({
  onLoad(opt) {
    id = opt.id
    this.getOrder()
  },
  getOrder() {
    wx.request({
      url: 'http://localhost:8080/api/orders/detail',
      method: 'POST',
      data: {
        id: id
      },
      success: (res) => {
        if (res.data.code == 200) {
          console.log('订单详情', res)
          this.setData({
            order: res.data.data
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          icon: 'non',
          title: '网络异常',
        })
      }
    })
  },
  /* 点击评分星 */
  pingfen(e) {
    this.setData({
      starNum: e.currentTarget.dataset.index + 1
    })
  },
  // 获取输入内容
  getContent(e) {
    this.setData({
      content: e.detail.value
    })
  },
  //提交评论
  submitComment() {
    let order = this.data.order;
    let content = this.data.content;
    let starNum = this.data.starNum;
    if (!starNum || starNum < 1) {
      wx.showToast({
        icon: 'error',
        title: '请评分',
      })
      return;
    }
    if (!content) {
      wx.showToast({
        icon: 'error',
        title: '评论内容为空',
      })
      return;
    }
    wx.request({
      url: 'http://localhost:8080/api/comment/add',
      method: 'POST',
      data: {
        ordersId:id,
        star:starNum,
        content:content,
        status:2
      },
      success: (res) => {
        if (res.data.code == 200) {
          console.log("评论成功", res)
          wx.showToast({
            title: '评论成功',
          })
          wx.navigateBack({
            delta: 0,
        })
        } else {
          console.log("评论失败", res)
          wx.showToast({
            icon: "none",
            title: '评论失败',
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          icon: "none",
          title: '网络异常',
        })

      }
    })
  },
})